/**
 *
 */
package de.lgohlke.helper;

import java.util.Collection;
import java.util.Iterator;

/**
 * <p>CollectionHelper class.</p>
 *
 * @author Lars Gohlke
 * @version $Id: $
 */
public final class CollectionHelper
{
  private CollectionHelper()
  {
    // ok
  }

  /**
   * prüft die Teilmengenbezieung ( ob A vollständig in B)
   *
   * @param a a {@link java.util.Collection} object.
   * @param b a {@link java.util.Collection} object.
   * @return a boolean.
   */
  @SuppressWarnings({ "rawtypes", "unchecked" })
  public static boolean isSubsetOf(final Collection<? extends Comparable> a, final Collection<? extends Comparable> b)
  {
    int count_in_a = 0;

    for (Comparable o_a : a)
    {
      boolean in_a = false;

      if (!in_a)
      {
        for (Comparable o_b : b)
        {
          in_a = o_a.compareTo(o_b) == 0;
          count_in_a += in_a ? 1 : 0;
        }
      }
    }

    return count_in_a == a.size();
  }

  /**
   * <p>concatArray.</p>
   *
   * @param array an array of {@link java.lang.Object} objects.
   * @param seperator a {@link java.lang.String} object.
   * @return a {@link java.lang.String} object.
   */
  public static String concatArray(final Object[] array, final String seperator)
  {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < (array.length - 1); i++)
    {
      buffer.append(array[i]);
      buffer.append(seperator);
    }
    return buffer.append(array[array.length - 1]).toString();

  }

  /**
   * prüft ob zwei Collections gleich sind (es gibt eine gleichnamige Methode in der ApacheCommons.CollectionUtils, die
   * ist aber bei einem Element in der Collection fehlerhaft)
   * <p/>
   * Reihenfolge wird beachtet
   *
   * @param a a {@link java.util.Collection} object.
   * @param b a {@link java.util.Collection} object.
   * @return a boolean.
   */
  public static boolean isEqualCollection(final Collection<?> a, final Collection<?> b)
  {
    boolean result = false;

    /**
     * one is null
     */
    if ((a == null) || (b == null))
    {
      if ((a == null) && (b == null))
      {
        result = true;
      }
      else
      {
        result = false;
      }
    }
    else
    {
      /**
       * same reference
       */
      if (a.equals(b))
      {
        result = true;
      }
      else
      {
        if (a.size() == b.size())
        {

          result = true;
          Iterator<?> iterA = a.iterator();
          Iterator<?> iterB = b.iterator();

          while (iterA.hasNext() && result)
          {

            Object elemA = iterA.next();
            Object elemB = iterB.next();

            if ((elemA instanceof Collection<?>) && (elemB instanceof Collection<?>))
            {
              result = isEqualCollection((Collection<?>) elemA, (Collection<?>) elemB);
            }
            else
            {
              if ((elemA instanceof Collection<?>) || (elemB instanceof Collection<?>))
              {
                // different elems
                result = false;
              }
              else
              {
                result = elemA.equals(elemB);
              }
            }
          }
        }
        else
        {
          result = false;
        }
      }

    }
    return result;
  }
}
